package cn.supermaster.management.web.service.system.impl;

import cn.supermaster.management.common.constant.UserConstant;
import cn.supermaster.management.common.page.TableDataInfo;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.common.utils.ShiroUtils;
import cn.supermaster.management.common.utils.WebHelper;
import cn.supermaster.management.common.utils.poi.ExcelUtil;
import cn.supermaster.management.system.domain.SystemPost;
import cn.supermaster.management.system.service.ISystemPostService;
import cn.supermaster.management.web.service.system.ISPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.List;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.service.system.impl
 * @ClassName: SPostRestServiceImpl
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/12 0:05
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class SPostServiceImpl implements ISPostService {

    private String prefix = "system/post";

    @Autowired
    private ISystemPostService postService;

    /*
     * ================================================
     *  页面跳转功能实现
     * ================================================
     */

    /**
     * @Description: [跳转岗位界面]
     * <p></P>
     * @Title: skipPostIndexHtml
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Return: java.lang.String
     */
    @Override
    public String skipPostIndexHtml() {
        return prefix + "/post";
    }

    /**
     * @Description: [跳转岗位新增界面]
     * <p></P>
     * @Title: skipAddPostHtml
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Return: java.lang.String
     * @Throws:
     */
    @Override
    public String skipAddPostHtml() {
        return prefix + "/add";
    }

    /**
     * @Description: [跳转岗位修改界面]
     * <p></P>
     * @Title: skipModifyPostHtml
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: modelMap
     * @Param: postId
     * @Return: java.lang.String
     * @Throws:
     */
    @Override
    public String skipModifyPostHtml(ModelMap modelMap, Long postId) {
        modelMap.put("post", postService.selectPostById(postId));
        return prefix + "/edit";
    }


    /*
     * ================================================
     *  Rest功能实现
     * ================================================
     */

    /**
     * @Description: [根据条件获取岗位信息列表]
     * <p></P>
     * @Title: queryPostListByCondition
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: post
     * @Return: cn.supermaster.management.common.page.TableDataInfo
     */
    @Override
    public TableDataInfo queryPostListByCondition(SystemPost post) {
        WebHelper.startPage();
        List<SystemPost> list = postService.selectPostList(post);
        return WebHelper.getDataTable(list);
    }

    /**
     * @Description: [导出岗位信息列表]
     * <p></P>
     * @Title: doExportPostList
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: post
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doExportPostList(SystemPost post) {
        List<SystemPost> list = postService.selectPostList(post);
        ExcelUtil<SystemPost> util = new ExcelUtil<SystemPost>(SystemPost.class);
        return util.exportExcel(list, "岗位数据");
    }

    /**
     * @Description: [删除岗位信息]
     * <p></P>
     * @Title: doRemovePostInformation
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: ids
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doRemovePostInformation(String ids) {
        try {
            return WebHelper.webResult(postService.deletePostByIds(ids));
        } catch (Exception e) {
            return WebHelper.error(e.getMessage());
        }
    }

    /**
     * @Description: [新增保存岗位]
     * <p></P>
     * @Title: doAddPostInformation
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: post
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doAddPostInformation(SystemPost post) {
        if (UserConstant.POST_NAME_NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return WebHelper.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstant.POST_CODE_NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return WebHelper.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(ShiroUtils.getuserName());
        return WebHelper.webResult(postService.insertPost(post));
    }

    /**
     * @Description: [修改保存岗位]
     * <p></P>
     * @Title: doModifyPostInformation
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: post
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doModifyPostInformation(SystemPost post) {
        if (UserConstant.POST_NAME_NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return WebHelper.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstant.POST_CODE_NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return WebHelper.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(ShiroUtils.getuserName());
        return WebHelper.webResult(postService.updatePost(post));
    }

    /**
     * @Description: [校验岗位名称]
     * <p></P>
     * @Title: checkPostNameUnique
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: post
     * @Return: java.lang.String
     */
    @Override
    public String checkPostNameUnique(SystemPost post) {
        return postService.checkPostNameUnique(post);
    }

    /**
     * @Description: [校验岗位编码]
     * <p></P>
     * @Title: checkPostCodeUnique
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: post
     * @Return: java.lang.String
     */
    @Override
    public String checkPostCodeUnique(SystemPost post) {
        return postService.checkPostCodeUnique(post);
    }
}